package ga.stopcriterias;

import ga.individuals.Individual;

import java.util.Collection;

public class StructureStrategy implements StopStrategy {
	private double similarAvg;
	
	public StructureStrategy(double similarAvg) {
		this.similarAvg = similarAvg;
	}
	
	@Override
	public boolean execute(Collection<Individual> population) {
		double sameIndividuals = 0;
		Individual prev;
		
		prev = null;
		
		for (Individual i : population) {
			if (prev != null) {
				if (prev.equals(i)) {
					sameIndividuals++;
				}
			} else {
				prev = i;
			}
		}
		
		return (sameIndividuals / population.size()) >= similarAvg;
	}
}
